package server

import (
	"SortSet/server/filter"
	"SortSet/server/handle"
	"SortSet/server/util"
	"fmt"
	"net/http"
	"time"
)

func Run() {
	fmt.Println("进入Server模式")
	http.HandleFunc("/api/data", filter.Log(filter.Err(handle.Data)))
	http.HandleFunc("/api/add", filter.Log(filter.Err(handle.Add)))
	http.HandleFunc("/api/remove", filter.Log(filter.Err(handle.Remove)))
	http.HandleFunc("/api/intersection", filter.Log(filter.Err(handle.Intersection)))
	http.HandleFunc("/api/union", filter.Log(filter.Err(handle.Union)))
	http.HandleFunc("/api/difference", filter.Log(filter.Err(handle.Difference)))
	// 静态文件处理
	files := http.FileServer(http.Dir("html/dist"))
	http.Handle("/", files)
	go func() {
		// 1秒后打开浏览器
		<-time.After(time.Second)
		fmt.Println("请访问 http://localhost:8080")
		util.OpenUrl("http://localhost:8080")
	}()
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println(err)
	}
}
